﻿//-----------------------------------------------------------------------
// <copyright file="ImageConverter.cs" company="Forebrain Technology, LLC">
//
// Copyright (c) Forebrain Technology, LLC. All rights reserved.
// 
// This source code is released under the Microsoft Public License (Ms-PL).
//
// </copyright>
//-----------------------------------------------------------------------
namespace PivotCollectionTools.Library
{
    using System.IO;
    using System.Security.Permissions;
    using Microsoft.DeepZoomTools;

    /// <summary>
    /// Covert an image into a Deep Zoom Image (DZI).
    /// </summary>
    public class ImageConverter
    {
        /// <summary>
        /// Used to create Deep Zoom Images (DZI).
        /// </summary>
        private ImageCreator converter;

        /// <summary>
        /// The destination folder for the converted images.
        /// </summary>
        private string destinationFolder;

        /// <summary>
        /// File extention for the converted images.
        /// </summary>
        private string fileExtension;

        /// <summary>
        /// Initializes a new instance of the <see cref="ImageConverter"/> class.
        /// </summary>
        /// <param name="destinationFolder">The destination folder.</param>
        public ImageConverter(string destinationFolder)
            : this(destinationFolder, 256, 0.92, ImageFormat.Jpg, 1, false)   
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ImageConverter"/> class.
        /// </summary>
        /// <param name="destinationFolder">The destination folder.</param>
        /// <param name="tileSize">Size of the tile.</param>
        /// <param name="imageQuality">The image quality.</param>
        /// <param name="imageFormat">The image format.</param>
        /// <param name="tileOverlap">The tile over lap.</param>
        /// <param name="smoothStreaming">if set to <c>true</c> [smooth streaming].</param>
        public ImageConverter(string destinationFolder, int tileSize, double imageQuality, ImageFormat imageFormat, int tileOverlap, bool smoothStreaming)
        {
            this.destinationFolder = destinationFolder;

            this.converter = new ImageCreator()
            {
                TileFormat = imageFormat,
                TileOverlap = tileOverlap,
                TileSize = tileSize,
                ImageQuality = imageQuality
            };

            if (smoothStreaming)
            {
                this.converter.ServerFormat = ServerFormats.SmoothStreaming;
            }

            this.fileExtension = this.converter.ServerFormat == ServerFormats.SmoothStreaming ? ".ism" : ".xml";
        }

        /// <summary>
        /// Converts the image.
        /// </summary>
        /// <param name="sourceImage">The source image.</param>
        /// <returns>A string representing the filie name of the converted image.</returns>
        [SecurityPermission(SecurityAction.LinkDemand, Unrestricted = true)]
        public string ConvertImage(string sourceImage)
        {
            string target = Path.Combine(this.destinationFolder, Path.GetFileNameWithoutExtension(sourceImage) + this.fileExtension);
            this.converter.Create(sourceImage, target);
            return target;
        }
    }
}
